// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "order_form_num": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "订单号",
    "label": "订单号"
  },
  "order_form_queue": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "订单排号",
    "label": "订单排号"
  },
  "user_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "用户",
    "label": "用户"
  },
  "type": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "订单角色",
    "label": "订单角色"
  },
  "status": {
    "rules": [
      {
        "format": "int"
      },
      {
        "minimum": -1,
        "maximum": 3
      },
      {
        "range": [
          {
            "text": "审核中",
            "value": 1
          },
          {
            "text": "待制作",
            "value": 2
          },
          {
            "text": "已完成",
            "value": 3
          },
          {
            "text": "已取消",
            "value": -1
          },
          {
            "text": "不通过",
            "value": -2
          }
        ]
      }
    ],
    "title": "订单状态",
    "label": "订单状态"
  },
  "no_pass_remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "不通过原因",
    "label": "不通过原因"
  },
  "total_fee": {
    "rules": [
      {
        "format": "int"
      },
      {
        "minimum": 0
      }
    ],
    "title": "总金额",
    "label": "总金额"
  },
  "dining_place": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "就餐场所",
    "label": "就餐场所"
  },
  "dining_person_num": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "就餐人数",
    "label": "就餐人数"
  },
  "region": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "苏州",
            "text": "苏州"
          },
          {
            "value": "温州",
            "text": "温州"
          }
        ]
      }
    ],
    "title": "订单地区",
    "label": "订单地区"
  },
  "dining_date": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "就餐时间",
    "label": "就餐时间"
  },
  "user_remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "用户备注",
    "label": "用户备注"
  },
  "merchant_remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "商家备注",
    "defaultValue": "",
    "label": "商家备注"
  },
  "create_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "创建时间",
    "label": "创建时间"
  },
  "update_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "更新时间",
    "label": "更新时间"
  }
}

const enumConverter = {
  "status_valuetotext": {
    "1": "审核中",
    "2": "待制作",
    "3": "已完成",
    "-1": "已取消",
    "-2": "不通过"
  },
  "region_valuetotext": {
    "苏州": "苏州",
    "温州": "温州"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
